package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.LoginService;
import com.javasm.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;

/**
 * @version 0.1
 * @className: LoginServlet
 * @description:
 * @author: gfs
 * @date: 2025/6/19 16:00
 * @since: jdk17
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1 接收请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //2 调用service
        LoginService loginService = new LoginServiceImpl();
        AdminUser loginUser = loginService.getUserByName(username);


        //3 根据执行情况 反馈信息
        ReturnResult returnResult = null ;
        if(loginUser!=null){
            if(loginUser.getIsvalid().equals(1)){
            //转成密文
                String pwdMD5 = "";
                try {
                    MessageDigest md5 = MessageDigest.getInstance("MD5");
                    md5.update(password.getBytes());
                    //32位16进制字符
                    pwdMD5 = new BigInteger(1, md5.digest()).toString(16);
                    System.out.println(pwdMD5);


                } catch (NoSuchAlgorithmException e) {
                    throw new RuntimeException(e);
                }
                //对比密文 如果原文一样 密文也一样
                if(loginUser.getPassword().equals(pwdMD5)){
                    //登录成功

                    //生成登录成功的票
                    HttpSession session = req.getSession();
                    //做登录访问控制使用
                    session.setAttribute("loginUser",loginUser);
                    //做模块权限控制List<String>
                    List<String> listUrl = loginService.listUserUrl(loginUser.getUid());
                    //存入session
                    session.setAttribute("loginUserUrl",listUrl);

                    returnResult =  new ReturnResult(ReturnCodeEnum.LOGIN_SUCCESS.getCode(),ReturnCodeEnum.LOGIN_SUCCESS.getMsg());

                    //1更新最后登录时间
                    loginService.updateUserLoginTime(loginUser.getUid());
                    //2把需要共享的信息存入缓存
                    //用户数据     loginUser
                    //用户动态菜单  loginUserMenus
                    List<AdminMenu> loginUserMenus = loginService.listMenuByUid(loginUser.getUid());
                    HashMap<String, Object> returnMap = new HashMap<>();
                    returnMap.put("loginUser",loginUser);
                    returnMap.put("loginUserMenus",loginUserMenus);
                    //附加数据
                    returnResult.setReturnData(returnMap);

                }else{
                    returnResult =  new ReturnResult(ReturnCodeEnum.NAMEPWD_ERROR.getCode(),ReturnCodeEnum.NAMEPWD_ERROR.getMsg());
                }
            }else{
                returnResult =  new ReturnResult(ReturnCodeEnum.USER_OFF.getCode(),ReturnCodeEnum.USER_OFF.getMsg());
            }

        }else{
            returnResult =  new ReturnResult(ReturnCodeEnum.NAMEPWD_ERROR.getCode(),ReturnCodeEnum.NAMEPWD_ERROR.getMsg());
        }

        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();




    }
}
